package com.sansan.qiangji.realize.device;

import com.sansan.qiangji.entity.device.BeltParamDetail;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.service.impl.device.BeltParamDetailServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BeltParamDetailReal {
    private BeltParamDetailServiceImpl beltParamDetailService;

    @Resource
    public void setBeltParamDetailService(BeltParamDetailServiceImpl beltParamDetailService){
        this.beltParamDetailService = beltParamDetailService;
    }

    @Transactional
    public void changeBeltParamDetailValue(String beltName, String beltParamName, Long beltParamChangeValue) {
        List<BeltParamDetail> beltParamDetails = beltParamDetailService.findByBeltName(beltName);
        beltParamDetails = beltParamDetails.stream().filter(e -> e.getBeltParamName().equals(beltParamName)).collect(Collectors.toList());
        if (beltParamDetails.size() == 0) {
            log.error("【修改传送带参数值】传送带参数不存在");
            throw new ServiceException(ResultEnum.BELT_PARAM_DETAIL_IS_NOT_EXIST);
        }
        if (beltParamDetails.get(0).getBeltParamType().equals("bool")) {
            if (beltParamChangeValue != 0 && beltParamChangeValue != 1) {
                log.error("【修改传送带参数值】传送带参数值只能是 0 或 1 !");
                throw new ServiceException(ResultEnum.BELT_PARAM_DETAIL_VALUE_ONLY_CAN_ZERO_OR_ONE);
            }
        }
        beltParamDetails.get(0).setBeltParamChangeValue(beltParamChangeValue);
        beltParamDetailService.updateBeltParamDetail(beltParamDetails.get(0));
    }
}
